package xdu.lz.stage4_atomic.chapter3_atomicReference;


import java.util.concurrent.atomic.AtomicReference;


public class AtomicReferenceTest {

    public static void main(String[] args) {

        Person p1 = new Person("z",1);

        Person p2 = new Person("z",2);

        AtomicReference<Person> reference = new AtomicReference<>(p1);

        System.out.println("p1的地址:"+p1.hashCode()+" 内容："+p1);

        //不是同一个引用，false
        System.out.println(reference.compareAndSet(new Person("z",1), p2));

        //是同一个引用，true
        System.out.println(reference.compareAndSet(p1, p2));

        Person p4 = reference.get();

        System.out.println("p1的地址:"+p1.hashCode()+" 内容："+p1);
        System.out.println("p2的地址:"+p2.hashCode()+" 内容："+p2);
        System.out.println("p4的地址:"+p4.hashCode()+" 内容："+p4);

        /**
         * AtomicReference作用是把原来的引用reference1，经过compareAndSet(reference1,reference2)变为引用reference2
         */
    }
}
